//
//  UIView+SizeFont.m
//  MMairs
//
//  Created by dongguozheng on 2017/3/1.
//  Copyright © 2017年 dongguozheng. All rights reserved.
//

#import "UIView+SizeFont.h"
#import <objc/runtime.h>
#define FontScaleKey @"FontScaleKey"
#define ScrenScale [UIScreen mainScreen].bounds.size.width/375
@implementation UIView (SizeFont)
/**
 设置字体大小比例
 
 @param value 需要设置的比例
 */
+ (void)setFontScale:(CGFloat)value{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@(value) forKey:FontScaleKey];
    [defaults synchronize];
}
+ (CGFloat)getFontScale{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber *valueNum = [defaults objectForKey:FontScaleKey];
    return valueNum?valueNum.floatValue:0;
}
@end
@interface UILabel (SizeFont)

@end
@interface UIButton (SizeFont)

@end
@interface UITextField (SizeFont)

@end
@implementation UILabel (SizeFont)

+ (void)load{
    if(!UILabelEnable) return;
    Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
    Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
    method_exchangeImplementations(imp, myImp);
    
    Method cmp = class_getInstanceMethod([self class], @selector(initWithFrame:));
    Method myCmp = class_getInstanceMethod([self class], @selector(myInitWithFrame:));
    method_exchangeImplementations(cmp, myCmp);
}

- (id)myInitWithCoder:(NSCoder*)aDecode {
    [self myInitWithCoder:aDecode];
    if (self) {
        //        //代码创建的时候 还不能拿到之后设置的tag 所以无法判断忽略项
        //        NSArray *ignoreTags = [UIView getIgnoreTags];
        //        for (NSNumber *num in ignoreTags) {
        //            if(self.tag == num.integerValue) return self;
        //        }
        CGFloat fontSize = self.font.pointSize;
        CGFloat scale = [UIView getFontScale];
        self.font = [self.font fontWithSize:fontSize*(scale?:ScrenScale)];
    }
    return self;
}


- (id)myInitWithFrame:(CGRect)frame{
    [self myInitWithFrame:frame];
    if(self){
        //        //代码创建的时候 还不能拿到之后设置的tag 所以无法判断忽略项
        //        NSArray *ignoreTags = [UIView getIgnoreTags];
        //        for (NSNumber *num in ignoreTags) {
        //            if(self.tag == num.integerValue) return self;
        //        }
        CGFloat fontSize = self.font.pointSize;
        CGFloat scale = [UIView getFontScale];
        self.font = [self.font fontWithSize:fontSize*(scale?:ScrenScale)];
    }
    return self;
}
@end
@implementation UIButton (SizeFont)

+ (void)load {
    if(!UIButtonEnable) return;
    
    Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
    Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
    method_exchangeImplementations(imp, myImp);
    
    Method cmp = class_getInstanceMethod([self class], @selector(initWithFrame:));
    Method myCmp = class_getInstanceMethod([self class], @selector(myInitWithFrame:));
    method_exchangeImplementations(cmp, myCmp);
}

- (id)myInitWithCoder:(NSCoder*)aDecode {
    [self myInitWithCoder:aDecode];
    if (self) {
        //        NSArray *ignoreTags = [UIView getIgnoreTags];
        //        for (NSNumber *num in ignoreTags) {
        //            if(self.tag == num.integerValue) return self;
        //        }
        CGFloat fontSize = self.titleLabel.font.pointSize;
        CGFloat scale = [UIView getFontScale];
        self.titleLabel.font = [self.titleLabel.font fontWithSize:fontSize*(scale?:ScrenScale)];
    }
    return self;
}

- (id)myInitWithFrame:(CGRect)frame{
    [self myInitWithFrame:frame];
    if(self){
        //        //代码创建的时候 还不能拿到之后设置的tag 所以无法判断忽略项
        //        NSArray *ignoreTags = [UIView getIgnoreTags];
        //        for (NSNumber *num in ignoreTags) {
        //            if(self.tag == num.integerValue) return self;
        //        }
        CGFloat fontSize = self.titleLabel.font.pointSize;
        CGFloat scale = [UIView getFontScale];
        self.titleLabel.font = [self.titleLabel.font fontWithSize:fontSize*(scale?:ScrenScale)];
    }
    return self;
}
@end

@implementation UITextField (SizeFont)

+ (void)load {
    if(!UITextFieldEnable) return;
    
    Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
    Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
    method_exchangeImplementations(imp, myImp);
    
    Method cmp = class_getInstanceMethod([self class], @selector(initWithFrame:));
    Method myCmp = class_getInstanceMethod([self class], @selector(myInitWithFrame:));
    method_exchangeImplementations(cmp, myCmp);
}

- (id)myInitWithCoder:(NSCoder*)aDecode {
    [self myInitWithCoder:aDecode];
    if (self) {
        //        NSArray *ignoreTags = [UIView getIgnoreTags];
        //        for (NSNumber *num in ignoreTags) {
        //            if(self.tag == num.integerValue) return self;
        //        }
        CGFloat fontSize = self.font.pointSize;
        CGFloat scale = [UIView getFontScale];
        self.font = [self.font fontWithSize:fontSize*(scale?:ScrenScale)];
    }
    return self;
}

- (id)myInitWithFrame:(CGRect)frame{
    [self myInitWithFrame:frame];
    if(self){
        //        //代码创建的时候 还不能拿到之后设置的tag 所以无法判断忽略项
        //        NSArray *ignoreTags = [UIView getIgnoreTags];
        //        for (NSNumber *num in ignoreTags) {
        //            if(self.tag == num.integerValue) return self;
        //        }
        CGFloat fontSize = self.font.pointSize;
        CGFloat scale = [UIView getFontScale];
        self.font = [self.font fontWithSize:fontSize*(scale?:ScrenScale)];
    }
    return self;
}

@end
